package com.util;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.webserver.WebServerServletTokenUtil;
import com.liferay.portlet.shopping.model.ShoppingItem;
import com.liferay.util.portlet.PortletProps;
import com.slayer.model.ShoppingCategoryExt;


public class ImageHelper {
	public static String getImageURL(ThemeDisplay themeDisplay, Object input)
			throws PortalException, SystemException {
		
		String imageURL = null;
		
		if (Validator.isNotNull(input)) {
			
			boolean isItem = (input instanceof ShoppingItem);

			imageURL = isItem? PortletProps.get("default.item.image.url") : PortletProps.get("default.category.image.url");
			
			String _imageURL = null;
			long _imageId = 0l;
			
			if (isItem) {
				ShoppingItem shoppingItem = (ShoppingItem) input;
				_imageURL = shoppingItem.getSmallImageURL();
				_imageId = shoppingItem.getSmallImageId();
			} else {
				ShoppingCategoryExt shoppingCategoryExt = (ShoppingCategoryExt) input;
				_imageURL = shoppingCategoryExt.getImageURL();
				_imageId = shoppingCategoryExt.getImageId();
			}
			
			if (Validator.isNotNull(_imageURL)) {
				imageURL = _imageURL;
			} else if (_imageId > 0l) {
				imageURL = themeDisplay.getPathImage()
						+ "/shopping/item?img_id="
						+ _imageId
						+ "&t="
						+ WebServerServletTokenUtil.getToken(_imageId);
			}
		}
		
		return imageURL;
	}
}